package com.springboot.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: 
 * @Description:JedisClusterConfig
 * @Date: Create in  2018/6/14 16:28
 */
/*
 * @Configuration public class JedisClusterConfig {
 * 
 * 
 * @Bean public RedisTemplate<String, Object>
 * redisTemplate(RedisConnectionFactory redisConnectionFactory) {
 * 
 * RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
 * //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
 * redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
 * redisTemplate.setHashValueSerializer(new
 * GenericJackson2JsonRedisSerializer());
 * //使用StringRedisSerializer来序列化和反序列化redis的key
 * redisTemplate.setKeySerializer(new StringRedisSerializer());
 * redisTemplate.setHashKeySerializer(new StringRedisSerializer());
 * //开启事务:redisTemplate.setEnableTransactionSupport(true);我觉得一般用不到(
 * 该操作是为了执行一组命令而设置的) redisTemplate.setConnectionFactory(redisConnectionFactory);
 * return redisTemplate; }
 * 
 * @Autowired private RedisProperties redisProperties;
 * 
 * public JedisCluster getJedisCluster(){ String []
 * serverArray=redisProperties.getClusterNodes().split(","); Set<HostAndPort>
 * nodes=new HashSet<>();
 * 
 * for (String ipPort:serverArray){ String [] ipPortPair=ipPort.split(":");
 * nodes.add(new
 * HostAndPort(ipPortPair[0].trim(),Integer.valueOf(ipPortPair[1].trim())));
 * 
 * } return new JedisCluster(nodes,redisProperties.getCommandTimeout()); }
 * 
 * }
 */